package com.ffcc.fitness.course.controller;

import com.ffcc.fitness.base.response.JsonResult;
import com.ffcc.fitness.course.pojo.dto.CourseInsertParam;
import com.ffcc.fitness.course.pojo.dto.CourseUpdateParam;
import com.ffcc.fitness.course.pojo.vo.CourseVO;
import com.ffcc.fitness.course.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("v1/course/")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("select")
    public JsonResult select() {
        List<CourseVO> courseVOList = courseService.select();
        return JsonResult.ok(courseVOList);
    }

    @PostMapping("insert")
    public JsonResult insert(CourseInsertParam courseInsertParam) {
        courseService.insert(courseInsertParam);
        return JsonResult.ok();
    }    // 更新课程卡片
    @PostMapping("update")
    public JsonResult update(@RequestBody CourseUpdateParam courseUpdateParam) {
        if (courseUpdateParam.getCourseId() == null || courseUpdateParam.getCourseId() <= 0) {
            return new JsonResult(5003, "课程ID不能为空或无效");
        }
        if (courseUpdateParam.getCoachId() == null || courseUpdateParam.getCoachId() <= 0) {
            return new JsonResult(5003, "教练ID不能为空或无效");
        }
        
        try {
            courseService.update(courseUpdateParam);
            return JsonResult.ok("更新成功");
        } catch (Exception e) {
            log.error("更新课程失败", e);
            return new JsonResult(5001, e.getMessage());
        }
    }

    // 删除课程卡片
    @PostMapping("delete")
    public JsonResult delete(@RequestParam Long courseId, @RequestParam Long coachId) {
        if (courseId == null || courseId <= 0) {
            return new JsonResult(5003, "课程ID不能为空或无效");
        }        if (coachId == null || coachId <= 0) {
            return new JsonResult(5003, "教练ID不能为空或无效");
        }
        
        try {
            courseService.delete(courseId, coachId);
            return JsonResult.ok("删除成功");
        } catch (Exception e) {
            log.error("删除课程失败", e);
            return new JsonResult(5001, e.getMessage());
        }
    }
}
